<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>菜单管理系统</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: "Segoe UI", sans-serif;
            background-color: #f4f6f9;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 40px;
        }

        .container {
            width: 100%;
            max-width: 900px;
            background-color: #fff;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
            display: flex;
            flex-direction: column;
            gap: 30px;
        }

        h2, h3 {
            color: #333;
            text-align: center;
        }

        .form-container {
            background-color: #f9f9f9;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.05);
        }

        .form-container input {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        .form-container label {
            font-weight: bold;
            display: block;
            margin-top: 10px;
        }

        .form-container button {
            margin-top: 20px;
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            border: none;
            border-radius: 4px;
            background-color: #6c757d;
            color: white;
        }

        .form-container button:hover {
            background-color: #5a6268;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }

        th, td {
            text-align: left;
            padding: 12px;
            border-bottom: 1px solid #ddd;
        }

        th {
            background-color: #007bff;
            color: white;
        }

        tr:hover {
            background-color: #f1f1f1;
        }

        .action-buttons button {
            margin-right: 5px;
            padding: 6px 10px;
            font-size: 14px;
            border-radius: 4px;
            border: none;
            cursor: pointer;
        }

        .edit-btn {
            background-color: #ffc107;
            color: black;
        }

        .delete-btn {
            background-color: #dc3545;
            color: white;
        }
    </style>
</head>
<body>

<div class="container">

    <!-- 菜单列表 -->
    <div>
        <h3>📁 菜单列表</h3>
        <table id="menuTable">
            <thead>
            <tr>
                <th>菜单ID</th>
                <th>菜单名称</th>
                <th>菜单链接</th>
                <th>父级菜单ID</th>
                <th>排序顺序</th>
                <th>备注</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody></tbody>
        </table>
    </div>

    <!-- 表单区域 -->
    <div class="form-container">
        <input type="hidden" id="menuId">
        <label for="menuName">菜单名称：</label>
        <input type="text" id="menuName">

        <label for="menuUrl">菜单链接：</label>
        <input type="text" id="menuUrl">

        <label for="parentId">父级菜单ID：</label>
        <input type="number" id="parentId" placeholder="可为空">

        <label for="sortOrder">排序顺序：</label>
        <input type="number" id="sortOrder" value="0">

        <label for="remake">备注：</label>
        <input type="text" id="remake">

        <button onclick="saveMenu()">保存菜单</button>
        <button onclick="clearForm()">清空表单</button>
    </div>

</div>

<script>
    $(document).ready(function () {
        loadMenus();
    });

    function loadMenus() {
        $.ajax({
            url: "/api/menus",
            method: "GET",
            success: function (data) {
                var tbody = $("#menuTable tbody");
                tbody.empty();
                data.forEach(function (m) {
                    tbody.append(`
                    <tr>
                        <td>${m.menuId}</td>
                        <td>${m.menuName}</td>
                        <td>${m.menuUrl || '无'}</td>
                        <td>${m.parentId || '无'}</td>
                        <td>${m.sortOrder}</td>
                        <td>${m.remake || '无'}</td>
                        <td class="action-buttons">
                            <button class="edit-btn" onclick='editMenu(${JSON.stringify(m)})'>编辑</button>
                            <button class="delete-btn" onclick='deleteMenu(${m.menuId})'>删除</button>
                        </td>
                    </tr>
                `);
                });
            }
        });
    }

    function saveMenu() {
        var menu = {
            menuId: $("#menuId").val(),
            menuName: $("#menuName").val(),
            menuUrl: $("#menuUrl").val(),
            parentId: $("#parentId").val() === "" ? null : parseInt($("#parentId").val()),
            sortOrder: $("#sortOrder").val(),
            remake: $("#remake").val()
        };

        var url = "/api/menus";
        var method = menu.menuId ? "PUT" : "POST";

        if (menu.menuId) {
            url += "/" + menu.menuId;
        }

        $.ajax({
            url: url,
            method: method,
            contentType: "application/json",
            data: JSON.stringify(menu),
            success: function () {
                loadMenus();
                clearForm();
            }
        });
    }

    function editMenu(menu) {
        $("#menuId").val(menu.menuId);
        $("#menuName").val(menu.menuName);
        $("#menuUrl").val(menu.menuUrl);
        $("#parentId").val(menu.parentId || "");
        $("#sortOrder").val(menu.sortOrder);
        $("#remake").val(menu.remake);
    }

    function deleteMenu(id) {
        if (confirm("确定要删除吗？")) {
            $.ajax({
                url: "/api/menus/" + id,
                method: "DELETE",
                success: function () {
                    loadMenus();
                }
            });
        }
    }

    function clearForm() {
        $("#menuId").val("");
        $("#menuName").val("");
        $("#menuUrl").val("");
        $("#parentId").val("");
        $("#sortOrder").val("0");
        $("#remake").val("");
    }
</script>

</body>
</html>